'''
Created on Mar 14, 2015

@author: eyaomai
'''
import os
import traceback
from com.stocklens.stock.common.utils import Logging
from com.stocklens.stock.data.crawlercommon import CrawlerManager, CrawlerBase

class MASCrawlerManager(CrawlerManager):
    def __init__(self, json_config_file, minimalThread):
        super(MASCrawlerManager, self).__init__(json_config_file, 0.1, None, minimalThread)
        self._masTitle = ''
    def _getNonCommonConfig(self, config):
        self.__emailList = [str(x) for x in config[MASConstant.CONF_FILE_EMAIL_LIST]]

    def _generateTask(self, task, checkTaskList=True):
        if super(MASCrawlerManager, self)._generateTask(task, True) is False:
            return False
        task.update(self._taskList.pop(0))
        task[MASConstant.PARA_SEND_MAIL] = self.sendMail        
        return True
    
    def sendMail(self, prefix, infoList):
        if infoList is None or len(infoList)==0:
            return
        try:
            for info in infoList:
                if self.debug is True:
                    self.logger.debug('Plan to send mail:"[%s]%s(%s)":%s', 
                                      self._masTitle+'|'+prefix,
                                      info.brief.encode(MASConstant.ENCODE_METHOD),
                                      info.releaseDate.encode(MASConstant.ENCODE_METHOD),
                                      info.link.encode(MASConstant.ENCODE_METHOD))
                else:
                    for emailAddress in self.__emailList:
                        echoStr = 'echo "%s" | mail -s "[%s]%s(%s)" %s' % (
                                                                  info.link.encode(MASConstant.ENCODE_METHOD), 
                                                                  self._masTitle+'|'+prefix,
                                                                  info.brief.encode(MASConstant.ENCODE_METHOD),
                                                                  info.releaseDate.encode(MASConstant.ENCODE_METHOD),
                                                                  emailAddress)
                        os.popen(echoStr)
        except Exception:
            traceInfo = traceback.format_exc()
            self.logger.warn('Fail to send mail:%s', traceInfo)    

class MASConstant(object):
    ENCODE_METHOD ='utf8'
    PARA_PREFIX = 'prefix'
    CONF_FILE_EMAIL_LIST = 'emailList'
    PARA_SEND_MAIL = 'sendMail'
    PARA_URL = 'url'

class MASCrawlerBase(CrawlerBase):
    def __init__(self, controller, dbProxy, request, maxItem, loggerName=None):
        super(MASCrawlerBase, self).__init__(controller, dbProxy, request)
        self.__maxItem = maxItem
        self.__isfirst = True
        self.__lastTitles = list()
        self.__sendMail = request[MASConstant.PARA_SEND_MAIL]
        self._url = request[MASConstant.PARA_URL]
        self.__prefix = request[MASConstant.PARA_PREFIX]
        self.__infoList = list()
        if loggerName is None:
            self.logger = Logging.LOGGER
        else:
            self.logger = Logging.getLogger(loggerName)

    def run(self):
        super(MASCrawlerBase, self).run()
        while(self.isShutDown() is False):
            self.__infoList = list()
            self._crawl()
            self.__handleInfoList()
            self.controller.randomSleep()
    
    def _crawl(self):
        '''
        sub-class to define how to parse the web site
        '''
        
        if self.debug is True:
            import random
            removeItems = int(5*random.random()+1)
            if len(self.__lastTitles)>removeItems:
                self.logger.debug('Remove %d items', removeItems)
                self.__lastTitles = self.__lastTitles[:-removeItems]
        
    def _handleItem(self, releaseDate, brief , link):
        if brief not in self.__lastTitles:
            self.__infoList.append(MailContent(releaseDate, brief , link))
    
    def __handleInfoList(self):
        if len(self.__infoList)>0:
            exceed =len(self.__lastTitles) + len(self.__infoList) > self.__maxItem 
            if exceed>0:
                self.__lastTitles = self.__lastTitles[exceed:]                    
            for i in range(len(self.__infoList)-1,-1,-1):
                self.__lastTitles.append(self.__infoList[i].brief)
            if self.__isfirst:
                self.logger.info('Initial Info list: %s', '\n--'.join([x.brief for x in self.__infoList]))
                self.__isfirst = False
            else:
                self.logger.info('Plan to send: %s', '\n--'.join([x.brief for x in self.__infoList]))
                self.__sendMail(self.__prefix, self.__infoList)
class MailContent(object):
    def __init__(self, releaseDate, brief, link):
        self.releaseDate = releaseDate
        self.brief = brief
        self.link = link

